//special validation setup after DOM loaded
$(function() {
    //add class name show-msg-bubble-on-hover to show image alt text in a popup bubble
    $('.show-msg-bubble-on-hover').live('mouseover mouseout', function(event) {
        if (event.type === 'mouseover') {
            MsgBubble.show($(this), { offsetLeft: -9, hasImage: true });
        } else {
            MsgBubble.hide($(this));
        }
    });
});

var MsgBubble = {
    defaults: {
        offsetTop: 0,
        offsetLeft: 0,
        hasImage: false
    },
    show: function($image, args) {
        var sErrorMsg = $image.attr('alt');

        // convert images to HTML if any images are present in the alt attribute
        if (args.hasImage) {
            sErrorMsg = sErrorMsg.replace('[IMG]', '<img src="');
            sErrorMsg = sErrorMsg.replace('[/IMG]', '" alt="" />');
        }
        
        if (sErrorMsg) {
            var oImagePosition = $image.offset();
            var sMsgBubble = '<div class="msg-bubble ' + $image.attr('id') + '"><div class="msg-bubble-content">' + sErrorMsg + '</div><div class="msg-bubble-btm"></div></div>';
            var $msgBubble = $(sMsgBubble).appendTo('body');
            var nMsgHeight = $msgBubble.height();
            var nOffsetTop = this.defaults.offsetTop;
            var nOffsetLeft = this.defaults.offsetLeft;

            if (typeof args === 'object') {
                if (typeof args.offsetTop === 'number') {
                    nOffsetTop = args.offsetTop;
                }
                if (typeof args.offsetLeft === 'number') {
                    nOffsetLeft = args.offsetLeft;
                }
            }

            $msgBubble.hide();
            $image.attr('title', '');
            $image.attr('alt', '');

            $msgBubble.offset({
                top: -nMsgHeight + oImagePosition.top + $(window).scrollTop() + nOffsetTop,
                left: oImagePosition.left + $(window).scrollLeft() + nOffsetLeft
            });

            $msgBubble.fadeIn(200);
        }
    },
    hide: function($image) {
        var $msgBubble = $('.msg-bubble.' + $image.attr('id'));

        if ($msgBubble.length) {
            var sErrorMsg = $msgBubble.children('.msg-bubble-content').html();

            $image.attr('title', sErrorMsg);
            $image.attr('alt', sErrorMsg);

            $msgBubble.fadeOut(200, function() {
                $(this).remove();
            });
        }
    }
};

function ValidationFrameworkExtender() {
    ValidatedControlOnBlur = function(evt) { };

    ValidatorValidate = function(val, validationGroup, evt, nofocus) {
        val.isvalid = true;
        if (evt == null) {
            evt = window.event || window.Event;
        }
        if ((typeof (val.enabled) === "undefined" || val.enabled != false) && IsValidationGroupMatch(val, validationGroup) && typeof (val.evaluationfunction) === "function") {
            val.isvalid = val.evaluationfunction(val);
            if (nofocus != true && !val.isvalid && Page_InvalidControlToBeFocused == null && typeof (val.focusOnError) === "string" && val.focusOnError == "t") {
                ValidatorSetFocus(val, evt);
            }
        }
        ValidatorUpdateDisplay(val);
    };

    ValidatorOnChange = function(evt) {
        window.validatedControlChanging = true;
        if (evt == null) {
            evt = window.event || window.Event;
        }

        Page_InvalidControlToBeFocused = null;
        var targetedControl;
        if (evt == null) {
            targetedControl = null;
        } else if (typeof (evt.srcElement) !== "undefined" && evt.srcElement != null) {
            targetedControl = evt.srcElement;
        } else {
            targetedControl = evt.target;
        }

        if (targetedControl != null) {
            if (targetedControl.value != "" || targetedControl.validatorInitialized) {
                var vals;
                if (typeof (targetedControl.Validators) !== "undefined") {
                    vals = targetedControl.Validators;
                } else if (targetedControl.tagName.toLowerCase() === "label") {
                    targetedControl = $get(targetedControl.htmlFor);
                    vals = targetedControl.Validators;
                }

                targetedControl.validatorInitialized = true;

                var i;
                for (i = 0; i < vals.length; i++) {
                    ValidatorValidate(vals[i], null, evt, true);
                }
            }
        }
        ValidatorUpdateIsValid();
        window.validatedControlChanging = false;
    };

    ValidtionReset = function(validtionGroup) {
        var i;
        window.cancelValidation = false; //Ensures that Validation will run
        if (typeof Page_BlockSubmit !== 'undefined') {
            Page_BlockSubmit = false;
        }

        if (typeof Page_ValidationSummaries !== "undefined") {
            for (i = 0; i < Page_ValidationSummaries.length; i++) {
                var summary = Page_ValidationSummaries[i];
                summary.parentNode.style.display = 'none'; //Hide the Error Summary
            }
        }

        if (typeof Page_Validators !== 'undefined') {
            for (i = 0; i < Page_Validators.length; i++) {
                var validator = Page_Validators[i];
                var vg = eval(validator.id + '.validationGroup');
                if (!validtionGroup || vg == validtionGroup) { //Is part of the specified validation group
                    validator.isvalid = true;
                    validator.hasrun = false; //this should be set to true only after a validator has executed
                    hideRequiredTemplates(validator.id);
                }
            }
        }
    };

    ValidateControl = function(controlId) {
        var isControlValid = true;

        if ($('#' + controlId).length) {
            var isPageValid = true;
            var validationGroup, validator;
            var i;

            for (i = 0; i < Page_Validators.length; i++) {
                validator = Page_Validators[i];
                if (validator.controltovalidate == controlId) {
                    validationGroup = validator.validationGroup;
                    validator.isvalid = true;
                    validator.hasrun = false;
                    ValidatorValidate(validator, validator.validationGroup, null);
                    isControlValid = validator.isvalid;
                    break;
                }
            }

            for (i = 0; i < Page_Validators.length; i++) {
                validator = Page_Validators[i];
                if (validator.validationGroup === validationGroup && validator.hasrun && !validator.isvalid) {
                    isPageValid = false;
                    break;
                }
            }

            Page_IsValid = isPageValid;
            ValidationSummaryOnSubmit(validationGroup);
        }

        return isControlValid;
    };

    ValidateControls = function(controlIds) {
        var isControlValid = true;
        var allValid = true;
        var validationGroup = [];
        var i;

        for (i = 0; i < controlIds.length; i++) {
            isControlValid = ValidateControl(controlIds[i], false);
            allValid = allValid && isControlValid;
        }

        return allValid;
    };

    function hideRequiredTemplates(validatorId) {
        var validatorContainer = null;
        var senderId = validatorId + "_ValidatorObject";
        try {
            validatorContainer = eval(senderId);
        } catch (e) { }
        if (validatorContainer && validatorContainer.TemplateBehavior && validatorContainer.TemplateBehavior.length) {
            var templateField = $get(validatorContainer.TemplateBehavior[0].clientId);
            if (templateField != null) {
                templateField.style.display = 'none';
            }
        }

        if (ValidationFramework.ErrorCssClass) {
            $('#' + validatorId).next().removeClass(ValidationFramework.ErrorCssClass);
        }
    }

    var vff_oldWebForm_DoPostBackWithOptions = WebForm_DoPostBackWithOptions;

    WebForm_DoPostBackWithOptions = function(options, returnResult) {
        ValidtionReset(options.validationGroup);
        var validationResult = true;
        window.postBackSource = $get(options.eventTarget.replace(/\$/g, '_'));
        if (options.validation) {
            if (typeof (Page_ClientValidate) == 'function') {
                validationResult = Page_ClientValidate(options.validationGroup);
            }
        }
        window.cancelValidation = false;
        if (validationResult) {
            if ((typeof (options.actionUrl) !== "undefined") && (options.actionUrl != null) && (options.actionUrl.length > 0)) {
                theForm.action = options.actionUrl;
            }
            if (options.trackFocus) {
                var lastFocus = theForm.elements.__LASTFOCUS;
                if ((typeof (lastFocus) !== "undefined") && (lastFocus != null)) {
                    if (typeof (document.activeElement) === "undefined") {
                        lastFocus.value = options.eventTarget;
                    } else {
                        var active = document.activeElement;
                        if ((typeof (active) !== "undefined") && (active != null)) {
                            if ((typeof (active.id) !== "undefined") && (active.id != null) && (active.id.length > 0)) {
                                lastFocus.value = active.id;
                            } else if (typeof (active.name) !== "undefined") {
                                lastFocus.value = active.name;
                            }
                        }
                    }
                }
            }
        }

        window.integralValidation = false;
        window.postBackSource = null;
        if (validationResult == false) {
            var evt = window.event || window.Event;
            if (evt) {
                evt.returnValue = false;
            }
            if (typeof Page_BlockSubmit !== 'undefined') {
                Page_BlockSubmit = false;
            }
            return false;
        } else {
            if (returnResult) {
                var postBack = function() {
                    __doPostBack(options.eventTarget, options.eventArgument);
                };
                setTimeout(postBack, 0);
                return true;
            } else {
                __doPostBack(options.eventTarget, options.eventArgument);
            }
        }
    };
}

var vff_oldonload = window.onload;
if (vff_oldonload) {
    window.onload = function() {
        vff_oldonload();
        window.cancelValidation = false;
        ValidationFrameworkExtender();
    };
} else {
    window.onload = ValidationFrameworkExtender;
}
var Ignore = 0,
    Inclusive = 1,
    Exclusive = 2,
    All = 0,
    Any = 1;
var ComparisonOperator = {
    Equal: 1,
    GreaterThan: 2,
    GreaterThanEqual: 3,
    LessThan: 4,
    LessThanEqual: 5,
    NotEqual: 6
};
var ValidCardTypes = {
    AMEX: 1,
    MC: 1,
    RZVISA: 1,
    VISA: 1,
    DINNERSCLUB: 1,
    ENROUTE: 1,
    DISCOVER: 1,
    JCB: 1,
    FSCC: 1,
    BBYCC: 1
};
var CapsLockEnabled = false;

function capsError(capsEngaged) {
    if (capsEngaged) {
        CapsLockEnabled = true;
    } else {
        CapsLockEnabled = false;
    }
}

function MWJ_say_Caps(oC) {
    if (typeof (capsError) === 'string') {
        if (oC) {
            alert(capsError);
        }
    } else {
        capsError(oC);
    }
}

function capsDetect(e) {
    if (!e) {
        e = window.event;
    }
    if (!e) {
        MWJ_say_Caps(false);
        return;
    }
    var theKey = e.which ? e.which : (e.keyCode ? e.keyCode : (e.charCode ? e.charCode : 0));
    var theShift = e.shiftKey || (e.modifiers && (e.modifiers & 4));
    MWJ_say_Caps((theKey > 64 && theKey < 91 && !theShift) || (theKey > 96 && theKey < 123 && theShift));
}

var ValidationFramework = new function() {
    this.ErrorCssClass = null;
    var m_displayAllMessages = false;
    var m_resetFieldOnFailure = false;

    function isInt(str) {
        var res = removeLeadingZeros(str);
        var i = parseInt(res, 10);
        if ("NaN" == i) {
            return false;
        }
        i = i.toString();
        if (i != res) {
            return false;
        }
        return true;
    }

    function isFloat(str) {
        var num = removeFloatLeadingZeros(str.replace(',', '.'));
        num = removeFloatTrailingZeros(num);
        var i = parseFloat(num);
        if ("NaN" == i) {
            return false;
        }
        i = i.toString();
        if (i != num) {
            return false;
        }
        return true;
    }

    function isDate(dateStr) {
        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
        var matchArray = dateStr.match(datePat);
        if (matchArray == null) {
            return false;
        }
        month = matchArray[1];
        day = matchArray[3];
        year = matchArray[5];
        if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1 || day > 31) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
            return false;
        }
        if (month == 2) {
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day == 29 && !isleap)) {
                return false;
            }
        }
        return true;
    }

    function isInRange(lowerBoundType, upperBoundType, lowerBound, upperBound, value) {
        if (lowerBoundType > 0) {
            if (value < lowerBound) {
                return false;
            }
            if (lowerBoundType == 2 && value == lowerBound) {
                return false;
            }
        }
        if (upperBoundType > 0) {
            if (value > upperBound) {
                return false;
            }
            if (upperBoundType == 2 && value == upperBound) {
                return false;
            }
        }
        return true;
    }

    function isRegExValid(expr, targetValue) {
        if (targetValue == "" && window.integralValidation) {
            return true;
        }
        if (expr.test(targetValue)) {
            return true;
        } else {
            return false;
        }
    }

    function isDomainValid(domains, targetValue) {
        var found = false;
        var i;
        for (i = 0; i < domains.length; i++) {
            if (domains[i] == targetValue) {
                found = true;
                break;
            }
        }
        return found;
    }

    function isUpperCase(myString) {
        return (myString === myString.toUpperCase());
    }

    function containsCharacters(characterSet, containsCharactersType, value) {
        var ret = false;
        if (containsCharactersType == 1) {
            for (i = 0; i < characterSet.length; i++) {
                if (value.indexOf(characterSet.charAt(i).toString()) !== -1) {
                    ret = true;
                    break;
                }
            }
        } else {
            ret = true;
            for (i = 0; i < characterSet.length; i++) {
                if (value.indexOf(characterSet.charAt(i).toString()) === -1) {
                    ret = false;
                    break;
                }
            }
        }
        return ret;
    }

    function ValidatePasswordLength(typedPassword, minLength, maxLength) {
        if (typedPassword.length >= minLength && typedPassword.length <= maxLength) {
            return true;
        } else {
            return false;
        }
    }

    function ValidatePasswordMinCaracters(typedPassword, minChars) {
        var charCount = 0;
        var i;
        for (i = 0; i < typedPassword.length; i++) {
            var c = typedPassword.substring(i, i + 1);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                charCount = charCount + 1;
            }
        }
        if (charCount < minChars && minChars > 0) {
            return false;
        } else {
            return true;
        }
    }

    function ValidatePasswordMinDigits(typedPassword, minDigits) {
        var digitsCount = 0;
        var i;
        for (i = 0; i < typedPassword.length; i++) {
            var d = typedPassword.substring(i, i + 1);
            if (d >= '0' && d <= '9') {
                digitsCount = digitsCount + 1;
            }
        }
        if (digitsCount < minDigits && minDigits > 0) {
            return false;
        } else {
            return true;
        }
    }

    function ValidatePasswordMixedCase(typedPassword) {
        var lowerCase = false;
        var upperCase = false;
        var i;
        for (i = 0; i < typedPassword.length; i++) {
            var c = typedPassword.substring(i, i + 1);
            if (!(c >= '0' && c <= '9')) {
                if (isUpperCase(c)) {
                    upperCase = true;
                } else {
                    lowerCase = true;
                }
            }
            if (upperCase && lowerCase) {
                return true;
            }
        }
        return false;
    }

    function isValidCardType(cardNumber, cardType, validCardTypes) {
        if ((cardNumber.match(/^(34|37)/)) && (cardType == "AMEX") && ((validCardTypes.AMEX) != 0)) {
            return (15 == cardNumber.length);
        } else if ((cardNumber.match(/^(51|52|53|54|55)/)) && (cardType == "MC") && ((validCardTypes.MC) != 0)) {
            return (16 == cardNumber.length);
        } else if ((cardNumber.match(/^(4)/)) && (cardType == "VISA") && ((validCardTypes.VISA) != 0)) {
            return (13 == cardNumber.length || 16 == cardNumber.length);
        } else if ((cardNumber.match(/^(4619)/)) && (cardType == "RZVISA") && ((validCardTypes.RZVISA) != 0)) {
            return (13 == cardNumber.length || 16 == cardNumber.length);
        } else if ((cardNumber.match(/^(300|301|302|303|304|305|36|38)/)) && (cardType == "DINNERSCLUB") && ((validCardTypes.DINNERSCLUB) != 0)) {
            return (14 == cardNumber.length);
        } else if ((cardNumber.match(/^(2014|2149)/)) && (cardType == "ENROUTE") && ((validCardTypes.ENROUTE) != 0)) {
            return (15 == cardNumber.length);
        } else if ((cardNumber.match(/^(6011)/)) && (cardType == "DISCOVER") && ((validCardTypes.DISCOVER) != 0)) {
            return (16 == cardNumber.length);
        } else if ((cardNumber.match(/^(3)/)) && (cardType == "JCB") && ((validCardTypes.JCB) != 0)) {
            return (16 == cardNumber.length);
        } else if ((cardNumber.match(/^(2131|1800)/)) && (cardType == "JCB") && ((validCardTypes.JCB) != 0)) {
            return (15 == cardNumber.length);
        } else if ((cardNumber.match(/^(50284901|50284980)/)) && (cardType == "FSCC") && ((validCardTypes.FSCC) != 0)) {
            return true;
        } else if ((cardNumber.match(/^(50284900|50284990)/)) && (cardType == "BBYCC") && ((validCardTypes.BBYCC) != 0)) {
            return true;
        } else {
            return false;
        }
    }

    function ValidateCreditCard(cardNumber, cardType, validCardTypes) {
        cardNumber = cardNumber.replace(/ /gi, '');
        if (isValidCardType(cardNumber, cardType, validCardTypes)) {
            var CheckNumbers = [];
            var CardLength = cardNumber.length;
            var i;
            for (i = CardLength - 2; i >= 0; i = i - 2) {
                CheckNumbers[CheckNumbers.length] = (parseInt(cardNumber.charAt(i).toString(), 10) * 2);
            }
            var CheckSum = 0;
            var iCount;
            for (iCount = 0; iCount <= CheckNumbers.length - 1; iCount++) {
                var _count = 0;
                if (parseInt(CheckNumbers[iCount], 10) > 9) {
                    var _numLength = (parseInt(CheckNumbers[iCount], 10)).toString().length;
                    var x;
                    for (x = 0; x < _numLength; x++) {
                        _count = _count + parseInt((parseInt(CheckNumbers[iCount], 10)).toString().charAt(x).toString(), 10);
                    }
                } else {
                    _count = parseInt(CheckNumbers[iCount], 10);
                }
                CheckSum = CheckSum + _count;
            }
            var OriginalSum = 0;
            var y;
            for (y = CardLength - 1; y >= 0; y = y - 2) {
                OriginalSum = OriginalSum + parseInt(cardNumber.charAt(y).toString(), 10);
            }
            return (((OriginalSum + CheckSum) % 10) === 0);
        } else {
            return false;
        }
    }

    function removeLeadingZeros(val) {
        while (val.charAt(0) === '0' && val.length > 1) {
            val = val.substring(1, val.length);
        }
        return val;
    }

    function removeFloatLeadingZeros(val) {
        while (val.charAt(0) === '0' && val.charAt(1) !== '.' && val.charAt(1) !== ',' && val.length > 1) {
            val = val.substring(1, val.length);
        }
        return val;
    }

    function removeFloatTrailingZeros(val) {
        while (val.charAt(val.length - 1) === '0' && (val.indexOf('.') !== -1 || val.indexOf(',') !== -1) && val.indexOf('.') !== val.length - 2 && val.indexOf(',') !== val.length - 2) {
            val = val.substring(0, val.length - 1);
        }
        if (val.charAt(val.length - 1) === '0' && (val.indexOf('.') !== -1 || val.indexOf(',') !== -1) && (val.indexOf('.') === val.length - 2 || val.indexOf(',') === val.length - 2)) {
            val = val.substring(0, val.length - 2);
        }
        return val;
    }

    function showErrorOnImage(obj, errorMsg) {
        for (i = 0; i < obj.childNodes.length; i++) {
            if (obj.childNodes[i].tagName == "IMG") {
                obj.childNodes[i].alt = errorMsg;
                obj.childNodes[i].title = errorMsg;
            }
        }
    }

    function resetPasswordField() {
        var objs = document.getElementsByTagName("input");
        if (objs != null) {
            for (i = 0; i < objs.length; i++) {
                if (objs[i].type == "password") {
                    objs[i].value = "";
                }
            }
        }
    }

    function trim(str, chars) {
        return ltrim(rtrim(str, chars), chars);
    }

    function ltrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    function rtrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    }

    this.IntegralValidate = function() {
        window.integralValidation = true;
        var groups = [];
        var groupCount = 0;
        for (i = 0; i < Page_ValidationSummaries.length; i++) {
            Page_ValidationSummaries[i].innerHTML = "";
            if (typeof (Page_ValidationSummaries[i].validationGroup) !== "undefined" && Page_ValidationSummaries[i].validationGroup != "searchKeyword") {
                groups[groupCount] = Page_ValidationSummaries[i].validationGroup;
                groupCount++;
            }
        }
        var valid = Page_ClientValidate('');
        var result = valid;
        var n = 0;
        for (n = 0; n < groups.length; n++) {
            if (valid) {
                valid = Page_ClientValidate(groups[n]);
            } else {
                result = Page_ClientValidate(groups[n]);
            }
            if (!result) {
                valid = result;
            }
        }
        if (window.integralValidation && !valid) {
            var gSummary = null;
            var summary;
            for (i = 0; i < Page_ValidationSummaries.length; i++) {
                summary = Page_ValidationSummaries[i];
                if (typeof (summary.validationGroup) === "undefined" || summary.validationGroup == "") {
                    gSummary = Page_ValidationSummaries[i];
                    break;
                }
            }
            if (gSummary != null) {
                for (i = 0; i < Page_ValidationSummaries.length; i++) {
                    summary = Page_ValidationSummaries[i];
                    if (summary.validationGroup != "" && summary.validationGroup != "searchKeyword" && !IsSummaryEmpty(summary.innerHTML)) {
                        gSummary.innerHTML += summary.innerHTML;
                    }
                }
                HideNotice();
                gSummary.parentNode.style.display = "block";
                gSummary.style.display = "block";
            }
        }
        window.integralValidation = false;
        return valid;
    };

    function IsSummaryEmpty(text) {
        text = text.replace('\n\n', '').trim();
        if (text === "") {
            return true;
        }
        return false;
    }

    this.ValidationReturnPackage = function(a, b, c, d, e) {
        var m_valid = a;
        var m_restricted = b;
        var m_ignorePrevious = c;
        var m_errormsg = d;
        var m_errorcode = e;
        this.IsValid = function() {
            return m_valid;
        };
        this.IsRestricted = function() {
            return m_restricted;
        };
        this.IgnorePrevious = function() {
            return m_ignorePrevious;
        };
        this.ErrorMessage = function() {
            return m_errormsg;
        };
        this.ErrorCode = function() {
            return m_errorcode;
        };
    };

    this.AlwaysValidValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.NotNullValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        if ((value == null) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.NullValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        if ((value != null) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.EmptyStringValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        if (window.integralValidation) {
            return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
        }
        if (value == "") {
            return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(!negated, false, ignorePrevious, negated ? errorMsg : "", negated ? errorCode : "");
    };

    this.StringLengthValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, lowerBoundType, upperBoundType, lowerBound, upperBound) {
        if (!isInRange(lowerBoundType, upperBoundType, lowerBound, upperBound, value.length) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.Int32RangeValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, lowerBoundType, upperBoundType, lowerBound, upperBound) {
        if (window.integralValidation && value.length === 0) {
            return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
        }
        if (!isInt(value) || !isInRange(lowerBoundType, upperBoundType, lowerBound, upperBound, value)) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.DateTimeRangeValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, lowerBoundType, upperBoundType, lowerBound, upperBound) {
        if (!isDate(value) || !isInRange(lowerBoundType, upperBoundType, new Date(lowerBound), new Date(upperBound), new Date(value))) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.RegExValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, regEx) {
        if (!isRegExValid(regEx, value)) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.DomainValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, domains) {
        if (!isDomainValid(domains, value) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.CreditCardValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, cardType, validCardTypes) {
        if (!ValidateCreditCard(value, $get(cardType).value, validCardTypes) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.PasswordStrengthValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, minLength, maxLength, minChars, minDigits) {
        if ((!ValidatePasswordLength(value, minLength, maxLength)) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        if ((!ValidatePasswordMinCaracters(value, minChars)) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        if ((!ValidatePasswordMinDigits(value, minDigits))) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        if ((!ValidatePasswordMixedCase(value)) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.ContainsCharactersValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, characterSet, charContains) {
        if ((!containsCharacters(characterSet, charContains, value)) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, "");
    };

    this.ValidNumberValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        if (window.integralValidation && value.length === 0) {
            return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
        }
        if (!isInt(value) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.ValidDoubleValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious) {
        if (!isFloat(value) || negated) {
            return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.CompareValidator = function(sender, value, typeToCompare, negated, errorMsg, errorCode, restricted, ignorePrevious, elTarget, targetOperator) {
        elTarget = $get(elTarget);
        if (elTarget != null && elTarget.value != "" && value != "") {
            var val1 = null;
            var val2 = null;
            var i;
            for (i = 0; i < elTarget.Validators.length; i++) {
                if (elTarget.Validators[i].id == sender.id) {
                    continue;
                }
                if (!elTarget.Validators[i].isvalid) {
                    return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
                }
            }
            if (typeToCompare == "datetime") {
                val1 = new Date(value);
                val2 = new Date(elTarget.value);
            } else {
                val1 = value;
                val2 = elTarget.value;
            }
            switch (targetOperator) {
                case ComparisonOperator.Equal:
                    if ((!(val1 == val2)) || negated) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
                case ComparisonOperator.GreaterThan:
                    if ((!(val1 > val2)) || negated) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
                case ComparisonOperator.GreaterThanEqual:
                    if ((!(val1 >= val2)) || negated) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
                case ComparisonOperator.LessThan:
                    if ((!(val1 < val2)) || negated) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
                case ComparisonOperator.LessThanEqual:
                    if ((!(val1 <= val2)) || negated) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
                case ComparisonOperator.NotEqual:
                    if (((val1 == val2))) {
                        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                    }
                    break;
            }
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.ContextValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, elTarget, targetValue, sTargetValue, validators) {
        elTarget = $get(elTarget);
        if (elTarget != null) {
            if (((elTarget.type == "radio" || elTarget.type == "checkbox") && elTarget.checked == sTargetValue)) {
                return ValidationFramework.AndCompositeValidator(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, validators);
            } else if (elTarget.value == targetValue) {
                return ValidationFramework.AndCompositeValidator(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, validators);
            }
        }
        return new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
    };

    this.PhoneNumberValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, areaCodeField, prefixField, suffixField, extensionField, isRequired) {
        var ctrl1 = $get(areaCodeField);
        var ctrl2 = $get(prefixField);
        var ctrl3 = $get(suffixField);
        var ctrl4 = $get(extensionField);
        phoneValue1 = ctrl1 == null ? "" : ctrl1.value;
        phoneValue2 = ctrl2 == null ? "" : ctrl2.value;
        phoneValue3 = ctrl3 == null ? "" : ctrl3.value;
        phoneValue4 = ctrl4 == null ? "" : ctrl4.value;
        var isValid = false;
        if (!isRequired) {
            if (phoneValue1.length === 0 && phoneValue2.length === 0 && phoneValue3.length === 0 && phoneValue4.length === 0) {
                isValid = true;
            }
        }
        if (!isValid) {
            if (phoneValue1.match(/^\d{3}$/) && phoneValue2.match(/^\d{3}$/) && phoneValue3.match(/^\d{4}$/) && phoneValue4.match(/^.*$/)) {
                isValid = true;
            }
        }
        if (sender.controltovalidate == areaCodeField && phoneValue1.match(/^\d{3}$/)) {
            isValid = true;
        } else if (sender.controltovalidate == prefixField && phoneValue1.match(/^\d{3}$/) && phoneValue2.match(/^\d{3}$/)) {
            isValid = true;
        } else if (sender.controltovalidate == suffixField && phoneValue1.match(/^\d{3}$/) && phoneValue2.match(/^\d{3}$/) && phoneValue3.match(/^\d{4}$/)) {
            isValid = true;
        }
        ProcessDepdendantField(areaCodeField + "_ValidatorObject", errorMsg, isValid);
        ProcessDepdendantField(prefixField + "_ValidatorObject", errorMsg, isValid);
        ProcessDepdendantField(suffixField + "_ValidatorObject", errorMsg, isValid);
        if (isValid || sender.controltovalidate == extensionField) {
            if (isValid || phoneValue4 != "") {
                ProcessDepdendantField(extensionField + "_ValidatorObject", errorMsg, isValid);
            }
        }
        if (sender.controltovalidate != areaCodeField && fieldSender != null && fieldSender.ValidatorCalloutBehavior == null) {
            isValid = true;
        } else if (phoneValue4 != "") {
            if (sender.controltovalidate != extensionField) {
                isValid = true;
            }
        } else if (sender.controltovalidate != suffixField) {
            isValid = true;
        }
        return new ValidationFramework.ValidationReturnPackage(isValid, restricted, ignorePrevious, isValid ? "" : errorMsg, isValid ? "" : errorCode);
    };

    this.GiftCardValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, giftCardNumberField, securityCodeField, giftCardRegEx, securityCodeRegEx, isRequired, validateRequiredOnly) {
        var ctrl1 = $get(giftCardNumberField);
        var ctrl2 = $get(securityCodeField);
        var operation = "";
        giftCardValue1 = ctrl1 == null ? null : ctrl1.value;
        giftCardValue2 = ctrl2 == null ? null : ctrl2.value;
        var GiftCardNumberToDisplay = "";
        if (giftCardValue1 != null && giftCardValue1.toString().length >= 4) {
            GiftCardNumberToDisplay = "************" + giftCardValue1.toString().substring(giftCardValue1.toString().length - 4, giftCardValue1.toString().length);
        }
        var GiftCardIsANumber = !isNaN(giftCardValue1);
        if (validateRequiredOnly) {
            if (parseInt(ctrl1.getAttribute("rowIndex"), 10) === 0) {
                if (window.postBackSource != null && window.postBackSource.id.indexOf("BtnApplyToOrder") > -1 && (giftCardValue1 == "" || giftCardValue2 == "")) {
                    return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                }
                if (window.postBackSource != null && (window.postBackSource.id.indexOf("BtnCheckBalance") > -1 || window.postBackSource.id.indexOf("checkBalanceButton") > -1) && (giftCardValue1 == "" || giftCardValue2 == "")) {
                    return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                }
                if (window.postBackSource != null && window.postBackSource.id.indexOf("BtnSubmit") > -1 && ((giftCardValue1 == "" && giftCardValue2 != "") || (giftCardValue1 != "" && giftCardValue2 == ""))) {
                    return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                }
                return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, !negated ? "" : errorMsg, !negated ? "" : errorCode);
            } else {
                if (giftCardValue1 == "" && giftCardValue2 == "") {
                    return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, !negated ? "" : errorMsg, !negated ? "" : errorCode);
                }
                if (window.postBackSource != null && (window.postBackSource.id.indexOf("BtnCheckBalance") > -1 || window.postBackSource.id.indexOf("checkBalanceButton") > -1) && ((giftCardValue1 == "" && giftCardValue2 != "") || (giftCardValue1 != "" && giftCardValue2 == ""))) {
                    return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                }
                if (window.postBackSource != null && (window.postBackSource.id.indexOf("BtnSubmit") > -1 || window.postBackSource.id.indexOf("BtnApplyToOrder") > -1) && ((giftCardValue1 == "" && giftCardValue2 != "") || (giftCardValue1 != "" && giftCardValue2 == ""))) {
                    return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                }
                return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, !negated ? "" : errorMsg, !negated ? "" : errorCode);
            }
        } else {
            if (giftCardValue1 != "" && giftCardValue2 != "") {
                if (giftCardValue1.toString().match(giftCardRegEx) && !isNaN(giftCardValue1) && giftCardValue1.length > 0) {
                    if (giftCardValue2.toString().match(securityCodeRegEx) && !isNaN(giftCardValue2) && giftCardValue2.length > 0) {
                        return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, !negated ? "" : errorMsg + " " + GiftCardNumberToDisplay, !negated ? "" : errorCode);
                    }
                }
            } else {
                return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, !negated ? "" : errorMsg + " " + GiftCardNumberToDisplay, !negated ? "" : errorCode);
            }
        }
        return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg + " " + GiftCardNumberToDisplay, negated ? "" : errorCode);
    };

    function ProcessDepdendantField(controlid, errorMsg, isValid) {
        fieldSender = $get(controlid);
        if (fieldSender != null) {
            fieldSender.errormessage = isValid ? "" : errorMsg;
            $get(fieldSender.controltovalidate).style.borderColor = isValid ? "#b7b7b7" : "#b70023";
            if (fieldSender.ValidatorCalloutBehavior != null) {
                if (isValid) {
                    fieldSender.ValidatorCalloutBehavior.hide();
                    var di = fieldSender.ValidatorCalloutBehavior._ValidationPopupBehavior._element;
                    if (di != null) {
                        di.style.display = "none";
                        di.style.visibility = "hidden";
                    }
                } else {
                    fieldSender.ValidatorCalloutBehavior.show(true);
                }
            }
        }
    }

    this.ExpiryDateValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, monthCtrl, yearCtrl, expDaysValid, isRequired, validateRequiredOnly) {
        var ret = null;
        var month = parseInt($get(monthCtrl).value, 10);
        var year = parseInt($get(yearCtrl).value, 10);
        if (!isRequired) {
            if ($get(monthCtrl).value == "" || $get(yearCtrl).value == "" || month === 0 || year === 0 || isNaN(month) || isNaN(year)) {
                ret = new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "");
            }
        } else if ($get(monthCtrl).value == "" || $get(yearCtrl).value == "" || month === 0 || year === 0 || isNaN(month) || isNaN(year)) {
            ret = new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg);
        }
        if (!validateRequiredOnly) {
            if (ret == null) {
                var cardMonth = month;
                var cardYear = year;
                if (hasExpired(cardMonth, cardYear, expDaysValid)) {
                    ret = new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
                }
            }
        }
        if (ret == null) {
            ret = new ValidationFramework.ValidationReturnPackage(true, false, ignorePrevious, "", "");
        }
        if (window.postBackSource != null) {
            if (sender.controltovalidate != yearCtrl) {
                var yearid = $get(yearCtrl).parentNode.id;
                var templateField = $get(yearid + "_ctl00");
                if (templateField != null) {
                    templateField.style.display = (ret.IsValid() ? 'none' : 'inline');
                    if (!ret.IsValid()) {
                        showErrorOnImage(templateField, errorMsg);
                    }
                }
            }
        }
        return ret;
    };

    function getDaysInMonth(month, year) {
        var m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (month !== 2) {
            return m[month];
        }
        if (year % 4 !== 0) {
            return m[1];
        }
        if (year % 100 === 0 && year % 400 !== 0) {
            return m[1];
        }
        return m[1] + 1;
    }

    function hasExpired(cardMonth, cardYear, cexpW) {
        cardMonth -= 1;
        var daysInMonth = getDaysInMonth(cardMonth, cardYear);
        var lastDayCardIsValid = new Date(cardYear, cardMonth, daysInMonth);
        lastDayCardIsValid.setDate(lastDayCardIsValid.getDate() - cexpW);
        var today = new Date();
        if (today >= lastDayCardIsValid) {
            return true;
        } else {
            return false;
        }
    }

    this.WildcardValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, wildCardCharacter, minimumCharacter) {
        if (value != "") {
            if (value.substr(value.length - 1, 1) == wildCardCharacter) {
                if (value.length > minimumCharacter) {
                    return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, errorMsg, errorCode);
                }
            }
            return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, errorMsg, errorCode);
        }
        return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, errorMsg, errorCode);
    };

    this.DependencyValidator = function(sender, value, negated, restricted, errorMsg, errorCode, ignorePrevious, isRequired, strictDepdendency, dependencyControls) {
        var el = null;
        if (dependencyControls != null) {
            var i;
            for (i = 0; i < dependencyControls.length; i++) {
                el = $get(dependencyControls[i]);
                if (el == null) {
                    continue;
                }
                if (!strictDepdendency || typeof el.Validators === 'undefined' || el.Validators.length === 0) {
                    if (el.value.length === 0) {
                        return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                    }
                } else {
                    var j;
                    for (j = 0; j < el.Validators.length; j++) {
                        var validator = el.Validators[j];
                        if (validator == null) {
                            continue;
                        }
                        if (validator.id == sender.id) {
                            continue;
                        }
                        if (!validator.isvalid || (validator.isvalid && validator.hasrun && !ignorePrevious)) {
                            return new ValidationFramework.ValidationReturnPackage(negated, restricted, ignorePrevious, negated ? "" : errorMsg, negated ? "" : errorCode);
                        }
                    }
                }
            }
        }
        return new ValidationFramework.ValidationReturnPackage(!negated, restricted, ignorePrevious, negated ? errorMsg : "", negated ? errorCode : "");
    }

    this.AndCompositeValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, validators) {
        var isValid = true,
            inErrorMsg = "",
            inErrorCode = "";
        var i;
        for (i = 0; i < validators.length; i++) {
            var validator = validators[i];
            if (typeof (validator) === "undefined" || validator == null) {
                continue;
            }
            var ret = validator[0](sender, value, validator[1], validator[2], validator[3], validator[4], validator[5], validator[6], validator[7], validator[8], validator[9], validator[10], validator[11], validator[12]);
            ignorePrevious = ret.IgnorePrevious();
            if (!ret.IsValid()) {
                isValid = false;
                if (ret.IgnorePrevious()) {
                    inErrorMsg = "";
                    inErrorCode = "";
                }
                if (m_displayAllMessages) {
                    inErrorMsg += (inErrorMsg != "" ? "<br />" : "") + ret.ErrorMessage();
                    inErrorCode += (inErrorCode != "" ? "," : "") + ret.ErrorCode();
                } else if (inErrorMsg == "") {
                    inErrorMsg = ret.ErrorMessage();
                    inErrorCode = ret.ErrorCode();
                }
                if (ret.IsRestricted()) {
                    break;
                }
            }
        }
        if (inErrorMsg != "") {
            errorMsg = inErrorMsg;
            errorCode = inErrorCode;
        }
        return new ValidationFramework.ValidationReturnPackage(isValid, restricted, ignorePrevious, isValid ? "" : errorMsg, isValid ? "" : errorCode);
    };
    this.OrCompositeValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, orValidators) {
        var inErrorMsg = "",
            inErrorCode = "";
        var i;
        for (i = 0; i < orValidators.length; i++) {
            var validator = orValidators[i];
            if (typeof (validator) === "undefined" || validator == null) {
                continue;
            }
            var ret = validator[0](sender, value, validator[1], validator[2], validator[3], validator[4], validator[5], validator[6], validator[7], validator[8], validator[9], validator[10], validator[11], validator[12]);
            ignorePrevious = ret.IgnorePrevious();
            if (ret.IsValid()) {
                return new ValidationFramework.ValidationReturnPackage(true, restricted, ignorePrevious, "", "");
            } else if (!ret.IsRestricted()) {
                if (ret.IgnorePrevious()) {
                    inErrorMsg = "";
                    inErrorCode = "";
                }
                if (inErrorMsg != "" && m_displayAllMessages) {
                    inErrorMsg += "<br />" + ret.ErrorMessage();
                    inErrorCode += "," + ret.ErrorCode();
                } else if (inErrorMsg == "") {
                    inErrorMsg = ret.ErrorMessage();
                    inErrorCode = ret.ErrorCode();
                }
            } else {
                inErrorMsg = ret.ErrorMessage();
                inErrorCode = ret.ErrorCode();
            }
        }
        if (inErrorMsg != "") {
            errorMsg = inErrorMsg;
            errorCode = inErrorCode;
        }
        return new ValidationFramework.ValidationReturnPackage(false, restricted, ignorePrevious, errorMsg, errorCode);
    };

    this.PageValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, required, pageControls, cancelValidation) {
        var atLeastOne = window.integralValidation;
        if (!atLeastOne) {
            var i;
            for (i = 0; i < pageControls.length; i++) {
                var obj = $get(pageControls[i]);
                if (obj != null) {
                    if (obj.tagName.toLowerCase() === "select") {
                        if (obj.selectedIndex > 0) {
                            atLeastOne = true;
                            break;
                        }
                    } else if (obj.value.length > 0) {
                        atLeastOne = true;
                        break;
                    }
                }
            }
        }
        if (!atLeastOne && cancelValidation) {
            window.cancelValidation = true;
        }
        return new ValidationFramework.ValidationReturnPackage(atLeastOne, restricted, ignorePrevious, atLeastOne ? "" : errorMsg, atLeastOne ? "" : errorCode);
    };

    this.UniqueValueListValidator = function(sender, value, negated, errorMsg, errorCode, restricted, ignorePrevious, required, pageControls) {
        var values = [];
        var j = 0;
        var valid = true;
        var i;
        for (i = 0; i < pageControls.length; i++) {
            var obj = $get(pageControls[i]);
            if (obj != null) {
                if (obj.tagName.toLowerCase() === "input" && obj.type == "text" && obj.value != "") {
                    var k;
                    for (k = 0; k < j; k++) {
                        if (obj.value.toLowerCase() == values[k].toLowerCase()) {
                            valid = false;
                            break;
                        }
                    }
                    values[j] = obj.value;
                    j++;
                }
            }
            if (!valid) {
                break;
            }
        }
        window.cancelValidation = !valid;
        return new ValidationFramework.ValidationReturnPackage(valid, restricted, ignorePrevious, valid ? "" : errorMsg, valid ? "" : errorCode);
    };

    this.FireValidation = function(sender, args, builtValidator) {
        if (sender.hasrun) { //Don't run more than once
            return;
        }

        var cancel = false;
        var validator = null;
        var validatorContainer = null;
        if (!builtValidator) {
            var senderId = sender.id + "_ValidatorObject";
            try {
                validatorContainer = eval(senderId);
            } catch (e) { }
            if (validatorContainer == null) {
                return;
            }
            if (validatorContainer.Validators == null) {
                return;
            }
            m_displayAllMessages = validatorContainer.DisplayAllMessages;
            m_resetFieldOnFailure = validatorContainer.ResetFieldOnFailure;
            m_dynamicDisplay = validatorContainer.DynamicDisplay;
            m_labelField = validatorContainer.LabelBehavior;
            m_template = validatorContainer.TemplateBehavior;
            m_field = sender.nextSibling;
            validator = validatorContainer.Validators[0];
            if (validator == null) {
                return;
            }
        } else {
            validator = builtValidator;
        }
        var validatorObj = validator[0];
        var ret = null;
        if (validatorObj == null || window.cancelValidation == true) {
            args.IsValid = true;
            sender.errormessage = "";
            cancel = window.cancelValidation;
        } else {
            ret = validatorObj(sender, trim(args.Value), validator[1], validator[2], validator[3], validator[4], validator[5], validator[6], validator[7], validator[8], validator[9]);
            sender.hasrun = true; //Mark the Validator as being run
            args.IsValid = ret.IsValid();
            sender.errormessage = ret.ErrorMessage();
            if (!args.IsValid && ret.IgnorePrevious() && window.integralValidation) {
                window.cancelValidation = true;
            }
        }

        ActivateRequiredTemplates(args.IsValid, sender.errormessage);

        if (!args.IsValid && m_resetFieldOnFailure) {
            var field = sender.nextSibling;
            if (!field || field.type != "text") {
                field = sender.previousSibling;
            }
            if (field && field.type == "text") {
                field.value = "";
            }
        }

        if (IsInline(sender) && typeof (UpdateErrorNotice) === "function") {
            UpdateErrorNotice();
        } else {
            HideNotice();
        }
        if (!cancel && !IsInline(sender)) {
            ManageSummaries(sender.validationGroup, args.IsValid);
        }
    };

    function SendWebAnalytics(isValid, validator) {
        if (!isValid) {
            if (validator.previousErrorCode != validator.errorcode) {
                trackEvent.event('event.view', {
                    errorCodeList: 'CB-' + validator.errorcode,
                    conversion: 'event19'
                });
                validator.previousErrorCode = validator.errorcode;
            }
        } else {
            validator.previousErrorCode = "";
        }
    }

    function ManageSummaries(validationGroup, isValid) {
        if (typeof (Page_ValidationSummaries) !== "undefined" && (typeof validatedControlChanging === 'undefined' || !validatedControlChanging)) {
            var i;
            for (i = 0; i < Page_ValidationSummaries.length; i++) {
                var summary = Page_ValidationSummaries[i];
                if (IsSummaryVisible(validationGroup, summary, isValid)) {
                    HideNotice();
                    summary.parentNode.style.display = "block";
                    summary.style.display = "block";
                } else if (summary.style.display == "none") {
                    summary.parentNode.style.display = "none";
                }
            }
        }
    }

    function IsSummaryVisible(validationGroup, summary, isValid) {
        if (!window.integralValidation && validationGroup == summary.validationGroup && !isValid) {
            return true;
        }
        return false;
    }

    function HideNotice() {
        var obj = $get("errornotice");
        if (obj != null) {
            obj.style.display = "none";
        }
    }

    function IsInline(sender) {
        if (sender.ValidatorCalloutBehavior != null) {
            return true;
        }
        return false;
    }

    function ActivateRequiredTemplates(isValid, errorMsg) {
        if (typeof validatedControlChanging === 'undefined' || !validatedControlChanging || m_dynamicDisplay) {
            if (m_labelField != null) {
                var labelField = $get(m_labelField.clientId);
                if (labelField != null) {
                    labelField.className = labelField.className.replace(' ' + m_labelField.className, '') + (isValid ? '' : ' ' + m_labelField.className);
                }
            }
            if (m_template != null) {
                var templateField = $get(m_template[0].clientId);
                if (templateField != null) {
                    templateField.style.display = (isValid ? 'none' : 'inline');
                    if (!isValid) {
                        showErrorOnImage(templateField, errorMsg);
                    }
                }
            }
            if (m_field && ValidationFramework.ErrorCssClass) {
                if (isValid) {
                    $('#' + m_field.id).removeClass(ValidationFramework.ErrorCssClass);
                } else {
                    $('#' + m_field.id).addClass(ValidationFramework.ErrorCssClass);
                }
            }
        }
    }
} ();
